import xml.dom.minidom

import time

import math

import utils



alarminfo = {"K-40": "0", "U-238": "1","Am-241": "2", "Ba-133": "3","Th-232": "4"}

def GetSpeFromN42FileNuctech(filepath):
    str = ""
    with open(filepath) as f:
        for s in f.readlines():
            str += s.replace("&lt;", "<").replace("&gt;", ">")
    dom = xml.dom.minidom.parseString(str)
    root = dom.documentElement
    rad = root.getElementsByTagName('RadMeasurement')[0]
    at = rad.getElementsByTagName('StartDateTime')[0].childNodes[0].data.replace("T", " ")
    spectrum=rad.getElementsByTagName('Spectrum')[0]
    spe = spectrum.getElementsByTagName('ChannelData')[0].childNodes[0].data.strip().split(' ')
    return [int(i) for i in spe]


def GetAlarmFromN42File(filepath):
    str = ""
    with open(filepath) as f:
        for s in f.readlines():
            str += s.replace("&lt;", "<").replace("&gt;", ">")
    dom = xml.dom.minidom.parseString(str)
    root = dom.documentElement
    spectrum = root.getElementsByTagName('Measurement')[0].getElementsByTagName('Spectrum')[0]
    at = spectrum.getElementsByTagName('StartTime')[0].childNodes[0].data.replace("T", " ")
    spe = spectrum.getElementsByTagName('ChannelData')[0].childNodes[0].data.strip().split(' ')
    nuclides = []
    res = root.getElementsByTagName('Nuclide')
    for ns in res:
        item = {
            'name': ns.getElementsByTagName('NuclideName')[0].childNodes[0].data,
            'confidence': ns.getElementsByTagName('Confidence')[0].childNodes[0].data,
            'type': getType(ns.getElementsByTagName('NuclideName')[0].childNodes[0].data)
        }
        nuclides.append(item)
    return spe, nuclides

def getType(name):
    return alarminfo.get(name,0)




